<template>
  <div :class="className" :style="{height: height + 'px',width: width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'
import china from '../../../static/zh-cn'

export default {
  props: {
    title: {
      type: String,
      default: '全国数据统计'
    },
    subtext: String,
    sublink: String,
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: Number,
      default: 550
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      required: true
    },
    tips: {
      type: String,
      default: ''
    },
    visualText: {
      type: Array,
      default: _ => {
        return ['高', '低']
      }
    },
    visualValue: {
      type: Array,
      default: _ => {
        return [0, 10e2]
      }
    },
    toolbox: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null,
      sidebarElm: null,
      seriesList: []
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.initChart()
    if (this.autoResize) {
      this.__resizeHandler = debounce(() => {
        if (this.chart) {
          this.chart.resize()
        }
      }, 100)
      window.addEventListener('resize', this.__resizeHandler)
    }
    // 监听侧边栏的变化
    this.sidebarElm = document.getElementsByClassName('sidebar-container')[0]
    this.sidebarElm && this.sidebarElm.addEventListener('transitionend', this.sidebarResizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return false
    }
    if (this.autoResize) {
      window.removeEventListener('resize', this.__resizeHandler)
    }
    this.sidebarElm && this.sidebarElm.removeEventListener('transitionend', this.sidebarResizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    setOptions(data) {
      const option = {
        title: {
          text: this.title,
          subtext: this.subtext,
          sublink: this.sublink
        },
        tooltip: {
          trigger: 'item',
          formatter: `{b}<br/>{c} ${this.tips}`
        },
        toolbox: {
          show: this.toolbox,
          orient: 'vertical',
          left: 'right',
          top: 'center',
          feature: {
            dataView: { readOnly: false },
            restore: {},
            saveAsImage: {}
          }
        },
        visualMap: {
          min: this.visualValue[0],
          max: this.visualValue[1],
          text: this.visualText,
          realtime: false,
          calculable: true,
          inRange: {
            color: ['lightskyblue', 'yellow', 'orangered']
          }
        },
        series: [{
          name: this.title,
          type: 'map',
          mapType: 'CN',
          itemStyle: {
            normal: { label: { show: true }},
            emphasis: { label: { show: true }}
          },
          data: data
        }]
      }
      this.chart.setOption(option)
    },
    initChart() {
      // console.log(china)
      echarts.registerMap('CN', china)
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
